package Scrap;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class demo21_ArrayImplement {

	private static int array[][];

	// populating the multidimensional array
	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		System.out.println("Enter the number of rows:");
		int row = scan.nextInt();
		System.out.println("Enter the number of columns:");
		int column = scan.nextInt();

		array = new int[row][column];

		System.out.println("Enter Values in the array....:");

		for (int i = 0; i < row; i++) {

			for (int j = 0; j < column; j++) {

				int input = scan.nextInt();

				array[i][j] = input;   // make a mul-dim array
			}
		}

		displayArray(array, row, column);  

		removeZeroRowsColumns(array, row, column);
		scan.close();
	}

	private static void removeZeroRowsColumns(int[][] array2, int row,
			int column) {
		int row1 = 0;
		int column1 = 0;
		int sum1 = 0;
		int sum2 = 0;
		List<Integer> rowList = new ArrayList<Integer>();
		List<Integer> colList = new ArrayList<Integer>();
		
		

		while (row1 < row) {
			column1 = 0;
			sum1 = 0;
			while (column1 < column) {

				sum1 = sum1 + array2[row1][column1];
				System.out.println("sum1 "+sum1);
				//			System.out.println("row :: " + row1 + "column ::" + column1);
				column1++;
			}

			if (sum1 == 0) {
				rowList.add(row1);
			}
			row1++;
		}    //1st while ends
		
		

		column1 = 0;
		while (column1 < column) {

			row1 = 0;
			sum2 = 0;
			while (row1 < row) {

				sum2 = sum2 + array2[row1][column1];
				System.out.println("sum2 "+sum2);
				
				row1++;
			}

			if (sum2 == 0) {

				colList.add(column1);
			
			}

			column1++;
		}        //2nd while ends
		
		
		displayFinalArray(array, rowList, colList, row, column);

	}

private static void print(List<Integer> rowList) {
		
		
		Iterator iter=rowList.iterator();
		while(iter.hasNext()){
			
	System.out.println(iter.next());		
			
		}
		
	}

	private static void displayFinalArray(int[][] array2,
			List<Integer> rowList, List<Integer> colList, int row, int column) {

		int newarray[][] = new int[row - rowList.size()][column- colList.size()];
		int newrow = -1;
		int newcol = 0;
		int row1 = 0;
		int col1 = 0;
		while (row1 < row) {
			if (rowList.contains(row1)) {
				row1++;
				continue;
			}
			col1 = 0;
			newcol = 0;
			newrow++;
			while (col1 < column) {
				if (colList.contains(col1)) {
					col1++;
					continue;

				}

				newarray[newrow][newcol++] = array2[row1][col1];

				col1++;
			}

			row1++;
		}

		System.out.println();
		System.out.println();
		System.out.println();
		displayArray(newarray, row - rowList.size(), column - colList.size());

	}

	private static void displayArray(int[][] array2, int row, int column) {

		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {

				System.out.print(array2[i][j] + " ");
			}

			System.out.println();
		}

	}

}
